<script>
import Vue from 'vue'
import Vuex from 'vuex'

// 1. 安装插件 vue的底层会执行 Vue.install
// Vue.use(Vuex)

// 2. 创建对象
let localData;
let imageUrl;
let data
let code

const store = new Vuex.Store({
  state:{
    code,
    data,
    localData,
    imageUrl,
    sell:[],
    price:[],
    codeList:[],
    goodsByCode:[],
    sum:0
  },
  mutations:{
    setData(state,data){
      state.data=data
    },
    setLocalData(state,data){
      state.localData=data
    },
    setImageUrl(state,data){
      state.imageUrl=data
    },
    setCode(state,data){
      state.code=data
    },
    setSell(state,data){
      state.sell.push(data)
    },
    setSellTN(state,data){
      state.sell=data
    },
    setSellPrice(state,data){
      state.price.push(data)
    },
    setSellList(state,data){
      state.codeList=data
    },
    setSellGoods(state,data){
      state.goodsByCode=data
    },
  },
  actions:{
    // setSellTN(context,data){
    //   let d1=context.getters.getData
    //   let d2=context.getters.getSell
    //   let is=true
    //   for (let i in d2){
    //     if (d1.code===d2[i].code){
    //       d2[i].number++
    //       d2[i].total+=d1.price
    //       is=false
    //     }
    //   }
    //   if (is){
    //     context.commit("setSell",data)
    //   }else
    //     context.commit("setSellTN",d2)
    // }

  },
  getters:{
    getData(state){
      return state.data
    },
    getlocalData(state){
      return state.localData
    },
    getImageUrl(state){
      return state.imageUrl
    },
    getCode(state){
      return state.code
    },
    getSell(state){
      return state.sell
    },
    getSellPrice(state){
      return state.price
    },
    getSellList(state){
      return state.codeList
    },
    getSellGoods(state){
      return state.goodsByCode
    }
  },
  modules:{

  }

})

// 3. 导出store对象
export  default store

</script>
